import java.util.Scanner;

public class wt2 {
    public static int maxSubArraySum(int[] arr) {
        if (arr == null || arr.length == 0) {
            throw new IllegalArgumentException("Array must not be null or empty");
        }

        int maxSoFar = arr[0];
        int maxEndingHere = arr[0];

        for (int i = 1; i < arr.length; i++) {
            maxEndingHere = Math.max(maxEndingHere + arr[i], arr[i]);
            maxSoFar = Math.max(maxSoFar, maxEndingHere);
        }

        return maxSoFar;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 提示用户输入数组的元素，用空格分隔
        System.out.println("请输入数组的元素，用空格分隔（例如：1 -2 3 -2 5 1）:");
        String input = scanner.nextLine();

        // 将输入的字符串转换为整数数组
        String[] elements = input.split("\\s+");
        int[] array = new int[elements.length];
        for (int i = 0; i < elements.length; i++) {
            array[i] = Integer.parseInt(elements[i]);
        }

        // 计算最大子数组和
        int result = maxSubArraySum(array);

        // 输出结果
        System.out.println("最大子数组和为: " + result);

        scanner.close();
    }
}